package com.fist.eth.flutter_plugin_eth_wallet

import android.util.Log
import com.fist.eth.flutter_plugin_eth_wallet.utils.TokenContractUtils
import com.fist.eth.flutter_plugin_eth_wallet.utils.TokenManager
import com.fist.eth.flutter_plugin_eth_wallet.utils.TokenWalletUtils
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
import java.lang.Exception
import java.math.BigDecimal
import java.math.BigInteger

class FlutterPluginEthWalletPlugin : MethodCallHandler {
    companion object {
        @JvmStatic
        fun registerWith(registrar: Registrar) {
            val channelWallet = MethodChannel(registrar.messenger(), "walletUtils")
            channelWallet.setMethodCallHandler(FlutterPluginEthWalletPlugin())

            val channelContract = MethodChannel(registrar.messenger(), "contractUtils")
            channelContract.setMethodCallHandler(FlutterPluginEthWalletPlugin())
        }
    }

    override fun onMethodCall(call: MethodCall, result: Result) {
        Log.i("call method", call.method)
        val callArray = call.method.split(".")
        if (callArray.size != 2) return

        val methodType = callArray[0]
        val methodName = callArray[1]

        if (methodType == "wallet") {
            // 导入钱包
            if (methodName == "import") {
                val mnemonic = call.argument<String>("mnemonic")
                val password = call.argument<String>("password")
                if (mnemonic == null || password == null)
                    result.error("mnemonic and password cannot set be null", "", "")

                val manageInstance = TokenManager.instance
                if (manageInstance.wallet != null && manageInstance.wallet!!.mnemonic == mnemonic!!) {
                    result.success(true)
                    return
                }

                val wallet = TokenWalletUtils.importTokenWallet(mnemonic!!, password!!)
                manageInstance.setWallet(wallet)

                Log.i("wallet", wallet.address)

                result.success(true)
                return
            }
        }

        if (methodType == "contract") {
            if (methodName == "config") {
                val url = call.argument<String>("url")
                val contractAddress = call.argument<String>("contractAddress")
                val tokenName = call.argument<String>("tokenName")

                TokenContractUtils.config(url!!, contractAddress!!, tokenName!!)
                result.success(true)
                return
            }

            if (methodName == "transfer") {
                val password = call.argument<String>("password")
                val toAddress = call.argument<String>("toAddress")
                val value = call.argument<Double>("value")

                val bigValue = BigDecimal.valueOf(value!!).toBigInteger()
                result.success(ContractUtils.transfer(password!!, toAddress!!, bigValue).get().toJsonStr())
                return
            }

            if (methodName == "balance") {
                try {
                    result.success(ContractUtils.balance().get().toString())
                } catch (e: Exception) {
                    Log.e("balance", e.message)
                }
//                result.success("${TokenContractUtils.mTokenName} ----- ${TokenContractUtils.mAddress}")
//                result.success("balance")
                return
            }
        }


        result.notImplemented()
    }
}
